<template>
    <div style="min-width: 1100px; position: relative;">
        <!--<div style="position: absolute; right: 4%; top: -35px;"> </div>-->
        <a-col span="4" style="min-width: 236px; margin-right: 15px">
            <a-card style="height: 820px;">
                    <span slot="title">
                        系统资源
                    </span>
                <div class="apply-title-btn-wrap" style="margin-top: -54px">
                    <a @click="addResource"
                       v-if="hasPermission('sysresource:addSysResource')">新增
                    </a>
                    <a @click="delResource" class="adel"
                       v-if="hasPermission('sysresource:delSysResources')">删除
                    </a>
                </div>
                <div style="height: 680px;overflow:auto; margin-left: -10px" class="resourceTree">
                    <a-tree
                            showIcon
                            :autoExpandParent="autoExpandParent" @expand="onExpand"
                            :expandedKeys="expandedKeys"
                            :treeData='getResourceTreeCopy' @select="changeNode">
                        <!--<em slot="folder" class="iconfont folderk"></em>-->
                        <a-icon slot="folder" type="folder" />
                        <a-icon slot="copy"  type="file" style="color: #dde6f8; font-size: 18px; margin-right: 8px"/>
                        <!--<em slot="copy" class="iconfont file" style="color: #dde6f8; font-size: 18px; margin-right: 8px"></em>-->
                        <template slot="custom" slot-scope="{ expanded }">
                            <a-icon :type="expanded ? 'folder' : 'file'"
                                style="color: rgba(0, 0, 0, 0.65);font-size: 18px; margin-right: 8px"></a-icon>
                        </template>
                    </a-tree>
                </div>
            </a-card>
        </a-col>
        <a-col span="18">
            <a-card style="height: 820px;">
                <span slot="title">资源详细配置<a-button type="primary" @click="()=>{this.showAddTable = true}" style="height: 30px !important; line-height: 30px !important; float: right"
                                                   v-if="hasPermission('sysresource:addSysResource')">同步系统资源
        </a-button></span>
                <div class="formSearch" style="min-width: 500px">
                    <a-form :form="form" label-position="right">

                        <a-row>
                            <a-col span="12">
                                <a-form-item label="资源名称：" prop="name" v-bind="formStyle">
                                    <a-input
                                            placeholder="请输入资源名称" :disabled="!saveResourceBtnShow"
                                            v-decorator="[
                        'name',
                        {
                            rules: [
                                { required: true, message: '请输入汉字、字母、数字、_或-字符' },
                                { validator: this.verify.validateName.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                                    />
                                </a-form-item>
                            </a-col>
                            <a-col span="12">
                                <a-form-item label="英文简称：" prop="alias" v-bind="formStyle">
                                    <!--<a-input type="text" v-model="resourceForm.alias" :disabled="!saveResourceBtnShow"
                                             style="width: 400px" placeholder="限长30，英文字母，并且唯一"
                                             :maxlength='64' />-->
                                    <a-input
                                            placeholder="请输入英文简称" :disabled="!saveResourceBtnShow"
                                            v-decorator="[
                        'alias',
                        {
                            rules: [
                                { required: true, message: '请输入英文字母字符' },
                                { validator: this.verify.validateEName.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                                    />
                                </a-form-item>
                            </a-col>
                        </a-row>
                      <a-row>
                        <a-col span="12">
                        <a-form-item v-bind="formStyle" label="服务名称" required>
                          <a-select
                            placeholder="请选择服务名称" @change="getInstanceVersions"
                            v-decorator="['serverCode',  { rules: [{ required: true, message: '请选择服务名称' }] }]"
                          >
                              <a-select-option v-for="item in serList" :value="item.serverCode" :key="item.serverCode">{{item.serverName }}
                              </a-select-option>
                          </a-select>
                        </a-form-item>
                        </a-col>
                        <a-col span="12">
                          <a-form-item label="上级资源：" prop="parentId" v-bind="formStyle">
                            <!--<select-tree :treeList="getResourceTreeCopy" :selectedNode="parentResource"
                                         @selectTreeData="selectNodeId" v-if="saveResourceBtnShow"
                                         style="width: 400px" />-->
                            <a-tree-select
                              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                              :treeData="getResourceTreeCopy" v-if="saveResourceBtnShow"
                              v-decorator="['parentId']" allowClear>
                                        <span style="color: #08c" slot="title"
                                              slot-scope="{key, title}">{{title}}</span>
                            </a-tree-select>
                            <a-input type="text" v-model="currentParentResName"
                                     :maxlength='128' v-if="!saveResourceBtnShow"
                                     :disabled="!saveResourceBtnShow" style="width:250px" />
                          </a-form-item>
                        </a-col>

                      </a-row>
                      <a-row v-if="form.getFieldValue('type') === '1' && (form.getFieldValue('parentId') === undefined  || form.getFieldValue('parentId') === '' || form.getFieldValue('parentId') === null)">
                        <a-col span="12">
                          <a-form-item v-bind="formStyle" label="服务版本" required>
                            <a-select
                              placeholder="请选择服务版本"
                              v-decorator="['version',  { rules: [{ required: true, message: '请选择服务版本' }] }]"
                            >
                              <a-select-option v-for="(item,index) in verionList" :value="item" :key="index">{{ item }}</a-select-option>
                            </a-select>
                          </a-form-item>
                        </a-col>
                        <a-col span="12">
                          <a-form-item v-bind="formStyle" label="资源版本" required>
                            <a-select
                              placeholder="请选择资源版本"
                              v-decorator="['webVersion',  { rules: [{ required: true, message: '请选择资源版本' }] }]"
                            >
                              <a-select-option v-for="(item,index) in verionList" :value="item" :key="index">{{ item }}</a-select-option>
                            </a-select>
                          </a-form-item>
                        </a-col>
                      </a-row>
                        <a-row>
                          <a-col span="12">
                            <a-form-item label="相对路径：" prop="uri" v-bind="formStyle">
                              <!-- <a-input type="text" v-model="resourceForm.uri" :maxlength='128'
                                        :disabled="!saveResourceBtnShow" style="width: 400px" />-->
                              <a-input
                                placeholder="请输入相对路径" :disabled="!saveResourceBtnShow"
                                v-decorator="[
                        'uri',
                        {
                            rules: [
                                { required: this.form.getFieldValue('type') === '2', message: '请输入相对路径' },
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                              />
                            </a-form-item>
                          </a-col>
                          <a-col span="12">
                            <a-form-item label="授  权  码：" prop="permissions" v-bind="formStyle">
                              <!--<a-input type="text" v-model="resourceForm.permissions"
                                       placeholder="多个用逗号分隔" :maxlength='128' :disabled="!saveResourceBtnShow"
                                       style="width: 400px" />-->
                              <a-input
                                placeholder="多个用逗号分隔" :disabled="!saveResourceBtnShow"
                                v-decorator="[
                        'permissions',
                        {
                            rules: [
                                { required: this.form.getFieldValue('type') === '3', message: '请输入授权码' },
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                              />
                            </a-form-item>
                          </a-col>

                        </a-row>
                        <a-row>
                          <a-col span="12">
                            <a-form-item label="资源类型：" prop="type" v-bind="formStyle">
                              <a-select placeholder="选择资源类型"
                                        v-decorator="['type',  { rules: [{ required: true, message: '请选择资源类型' }] }]"
                                        :disabled="!saveResourceBtnShow">
                                <a-select-option v-for="item in resourceTypes" :value="item.value"
                                                 :key="item.value">{{ item.label }}
                                </a-select-option>
                              </a-select>
                            </a-form-item>
                          </a-col>
                          <a-col span="12">
                            <a-form-item label="路由类型：" prop="router" v-bind="formStyle">
                              <a-select placeholder="选择路由类型" :disabled="!saveResourceBtnShow"
                                        allowClear
                                        v-decorator="['router']">
                                <a-select-option v-for="item in routers" :value="item.value"
                                                 :key="item.value">{{
                                  item.label }}
                                </a-select-option>
                              </a-select>
                            </a-form-item>
                          </a-col>
                        </a-row>
                        <a-row>
                          <a-col span="12">
                            <a-form-item label="导航图标：" prop="icon" v-bind="formStyle">
                              <a-select placeholder="选择导航图标" allowClear
                                        :disabled="!saveResourceBtnShow" v-decorator="['icon']">
                                <a-select-option v-for="item in icons" :value="item.value"
                                                 :key="item.value"><em :class="'iconfont '+item.value" style="margin-right: 10px;"></em>{{item.label }}
                                </a-select-option>
                              </a-select>
                            </a-form-item>
                          </a-col>
                            <a-col span="12">
                                <a-form-item label="备注：" prop="description" v-bind="formStyle">
                                    <a-textarea type="textarea"
                                                :disabled="!saveResourceBtnShow"
                                                :autosize="{minRows: 2,maxRows: 10}"
                                                v-decorator="[
                        'description',
                        {
                            rules: [
                                { required: false },
                                { validator: this.verify.validateHundredName.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }]"
                                    />
                                </a-form-item>
                            </a-col>
                        </a-row>
                        <a-row>
                            <a-col span="24">
                                <a-col span="4"></a-col>
                                <a-col span="2">
                                    <a-checkbox
                                            :checked="authFlagFlag"
                                            @change="changeAuthFlag"><span
                                            style="margin-right: 8px;margin-top: 20px;">是否鉴权</span>
                                    </a-checkbox>
                                </a-col>
                                <a-col span="2">
                                    <a-checkbox
                                            v-if="auditCheckBoxShow"
                                            :checked="resourceForm.auditFlag"
                                            v-model="resourceForm.auditFlag"
                                            @change="changeAudit"><span
                                            style="margin-right: 8px;margin-top: 20px;">是否审计</span>
                                    </a-checkbox>
                                </a-col>
                                <a-col span="16" style="text-align: right;">
                                    <a-button type="default" v-if="saveResourceBtnShow"
                                              @click="handleReset('form')">重置
                                    </a-button>
                                    <a-button v-if="hasPermission('sysresource:enableSysResources')"
                                              v-show="resourceForm.id && resourceForm.validStatus==='0'"
                                              type="default"
                                              @click="enableResource">
                                        启用
                                    </a-button>
                                    <a-button v-if="hasPermission('sysresource:disableSysResources')"
                                              v-show="resourceForm.id&&resourceForm.validStatus==='1'"
                                              type="default" style="margin-left: 10px;"
                                              @click="disableResource">停用
                                    </a-button>
                                    <a-button type="primary" @click="saveResource" style="margin-left: 10px;" v-if="saveResourceBtnShow">保存
                                    </a-button>
                                </a-col>

                            </a-col>
                        </a-row>
                    </a-form>
                </div>
                <a-row v-show="resourceForm.id !== ''&&resourceForm.audit==='1'">
                    <div style="height: 1px;border-top: solid #e9eaec 1px;margin: 14px 0;"></div>
                    <div class="formSearch" style="min-width: 500px">
                        <a-form :form="form2">
                            <a-row>
                                <a-col span="12">
                                    <a-form-item label="审计分类：" prop="auditSort" v-bind="formStyle">
                                        <a-select placeholder="选择审计分类"
                                                  :disabled="!saveAuditBtnShow"
                                                  v-decorator="['auditSort',  { rules: [{ required: true, message: '请选择审计分类' }] }]">
                                            <a-select-option v-for="item in auditSorts" :value="item.value"
                                                             :key="item.value">{{ item.label }}
                                            </a-select-option>
                                        </a-select>
                                    </a-form-item>
                                </a-col>
                                <a-col span="12">
                                    <a-form-item label="操作类型：" prop="operateType" v-bind="formStyle">
                                        <a-select
                                                v-decorator="['operateType',  { rules: [{ required: true, message: '请选择操作类型' }] }]"
                                                placeholder="选择操作类型"
                                                :disabled="!saveAuditBtnShow">
                                            <a-select-option v-for="item in opTypes" :value="item.value"
                                                             :key="item.value">{{ item.label }}
                                            </a-select-option>
                                        </a-select>
                                    </a-form-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                <a-col span="12">
                                    <a-form-item label="主键参数序号：" prop="objidParamSeq" v-bind="formStyle">
                                        <a-input
                                                placeholder="主键参数序号" :disabled="!saveAuditBtnShow"
                                                v-decorator="[
                        'objidParamSeq',
                        {
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                                        />
                                    </a-form-item>
                                </a-col>
                                <a-col span="12">
                                    <a-form-item label="主键参数名：" prop="objidParamName" v-bind="formStyle">
                                        <a-input
                                                placeholder="主键参数名" :disabled="!saveAuditBtnShow"
                                                v-decorator="[
                        'objidParamName',
                        {
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                                        />
                                    </a-form-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                <a-col span="12">
                                    <a-form-item label="用户参数序号：" prop="authdevParamSeq" v-bind="formStyle">
                                        <a-input
                                                placeholder="用户参数序号" :disabled="!saveAuditBtnShow"
                                                v-decorator="[
                        'authdevParamSeq',
                        {
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                                        />
                                    </a-form-item>
                                </a-col>
                                <a-col span="12">
                                    <a-form-item label="用户参数名称：" prop="authdevParamName" v-bind="formStyle">
                                        <a-input
                                                placeholder="用户参数名称" :disabled="!saveAuditBtnShow"
                                                v-decorator="[
                        'authdevParamName',
                        {
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                                        />
                                    </a-form-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                <a-col span="12">
                                    <a-form-item label="屏蔽参数：" prop="shieldParams" v-bind="formStyle">
                                        <a-radio-group v-decorator="['shieldParams', { initialValue: '0' }]">
                                            <a-radio value="1" key="1" :disabled="!saveAuditBtnShow"><span>是</span>
                                            </a-radio>
                                            <a-radio value="0" key="0" :disabled="!saveAuditBtnShow"><span>否</span>
                                            </a-radio>
                                        </a-radio-group>
                                    </a-form-item>
                                </a-col>
                                <a-col span="12">
                                    <a-form-item label="审计类型：" prop="shieldParams" v-bind="formStyle">
                                        <a-radio-group v-decorator="['auditType', { initialValue: '1' }]">
                                            <a-radio value="1" key="1" :disabled="!saveAuditBtnShow"><span>人工审核</span>
                                            </a-radio>
                                            <a-radio value="0" key="0" :disabled="!saveAuditBtnShow"><span>自动审核</span>
                                            </a-radio>
                                        </a-radio-group>
                                    </a-form-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                <a-col span="4"></a-col>
                                <a-button type="default" style="margin-right:15px" v-if="saveAuditBtnShow"
                                          @click="handleReset('form2')">重置
                                </a-button>
                                <a-button type="primary" @click="saveAudit" v-if="saveAuditBtnShow">保存
                                </a-button>
                            </a-row>
                        </a-form>
                    </div>
                </a-row>
            </a-card>
        </a-col>
      <a-modal v-model="showAddTable" :width="640"
               title="同步"
               :maskClosable="false"
               :destroyOnClose="true" @cancel="cancelApply">
        <a-form :form="form3" @keydown.native.enter.prevent="saveApply" class="modelSearch">
          <a-form-item v-bind="formStyle" label="服务名称" required>
            <a-select
              placeholder="请选择服务名称" @change="getInstanceVersions"
              v-decorator="['serverCode',  { rules: [{ required: true, message: '请选择服务名称' }] }]"
            >
                <a-select-option v-for="item in serList" :value="item.serverCode" :key="item.serverCode">{{item.serverName }}
                </a-select-option>
            </a-select>
          </a-form-item>
          <a-form-item v-bind="formStyle" label="服务版本" required>
            <a-select
              placeholder="请选择服务版本"
              v-decorator="['version',  { rules: [{ required: true, message: '请选择服务版本' }] }]"
            >
              <a-select-option v-for="(item,index) in verionList" :value="item" :key="index">{{ item }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-form>
        <div slot="footer">
          <a-button type="default" @click="cancelApply">取消</a-button>
          <a-button type="primary" @click="saveApply">确定</a-button>
        </div>
      </a-modal>
    </div>
</template>

<script>
  import util from '@/libs/util.js'
  import verify from 'libs/verify'

  export default {
        components: {
        },
        data () {
            return {
                form: this.$form.createForm (this),
                form2: this.$form.createForm (this),
                form3: this.$form.createForm (this),
                serList:[],
                verionList:[],
                showAddTable: false,
                verify,
                resourceTree: [],
                autoExpandParent: true,
                routers: [],
                resourceTypes: [],
                icons: [],
                auditSorts: [],
                opTypes: [],
                parentResource: {},
                auditCheckedShow: false,
                currentParentResName: '',
                resourceForm: {
                    id: '',
                    name: '',
                    alias: '',
                    uri: '',
                    permissions: '',
                    router: '',
                    type: '',
                    parentId: '',
                    icon: '',
                    audit: '0',
                    auditFlag: false,
                    validStatus: '0',
                    description: ''
                },
                authFlagFlag:false,
                auditForm: {
                    id: '',
                    resId: '',
                    auditSort: '',
                    operateType: '',
                    objidParamSeq: '',
                    objidParamName: '',
                    authdevParamSeq: '',
                    authdevParamName: '',
                    shieldParams: '0'
                },
                auditDisabled: false,
                currentSelectedNodeId: '',
                buttonProps: {
                    type: 'ghost',
                    size: 'small'
                },
                formStyle: {
                    labelCol: {span: 8},
                    wrapperCol: {span: 16}
                },
                formStyleT: {
                    labelCol: {span: 4},
                    wrapperCol: {span: 20}
                },
                expandedKeys: []
            }
        },
        computed: {

            /**获取资源树**/
            getResourceTree () {
                return this.$store.state.sysResManager.resourceTree
            },
            /**获取资源树副本**/
            getResourceTreeCopy () {
                //todo 后端需要添加图表自定义属性
                let tree = this.$store.state.sysResManager.resourceTree
                tree = JSON.parse (JSON.stringify (tree))
                util.setTreeSelected (tree)
                return tree
            },
            /**审计checkbox框显示控制**/
            auditCheckBoxShow () {
                if (!this.resourceForm.auditFlag && !this.hasPermission ('sysresource:addResAuditDef')) {
                    return false
                }
                if (this.resourceForm.auditFlag && !this.hasPermission ('sysresource:delResAuditDef')) {
                    return false
                }
                return !( this.resourceForm.id === '' || this.resourceForm.type !== '3' )

            },

            /**资源保存按钮显示控制**/
            saveResourceBtnShow () {
                return this.hasPermission ('sysresource:addSysResource') || this.hasPermission ('sysresource:updSysResource')
            },

            /**审计保存按钮显示控制**/
            saveAuditBtnShow () {
                return this.hasPermission ('sysresource:addResAuditDef') || this.hasPermission ('sysresource:updResAuditDef')
            }
        },
        methods: {
            onExpand ( expandedKeys ) {
                this.expandedKeys = expandedKeys
                this.autoExpandParent = false
            },
            /**
             * Function    : loadPageDictDatas
             * Description : 加载数据字典
             * Input       : val
             * Return      : none
             */
            loadPageDictDatas () {
                // 路由类型
                this.$loadSysDicts (this.$dicts.sortCode.ROUTERS, this.routers)
                // 资源类型
                this.$loadSysDicts (this.$dicts.sortCode.RESOURCE_TYPES, this.resourceTypes)
                // 资源图标
                this.$loadSysDicts (this.$dicts.sortCode.ICONS, this.icons)
                // 审计分类
                this.$loadSysDicts (this.$dicts.sortCode.AUDIT_SORTS, this.auditSorts)
                // 操作类型
                this.$loadSysDicts (this.$dicts.sortCode.OP_TYPES, this.opTypes)
            },

            /**
             * Function    : loadResourceTree
             * Description : 加载左侧资源树和右侧资源详情
             * Input       : selectedId 选中的资源节点
             * Return      : none
             */
            loadResourceTree ( selectedId ) {
                this.$store.dispatch ('loadResourceTreeAct', {selectedId}).then (( id ) => {
                    if (id) {
                        // 加载右边的资源
                        this.currentSelectedNodeId = id
                        this.expandedKeys.push (id)
                        this.loadResourceInfo (id)
                    }
                })
            },

            /**
             * Function    : loadResourceInfo
             * Description : 加载左侧资源树和右侧资源详情
             * Input       : none
             * Return      : none
             */
            loadResourceInfo ( id ) {
                this.form.resetFields ()
                this.$store.dispatch ('loadResourceAct', {id}).then (( response ) => {
                    this.resourceForm = response.data
                    this.resourceForm.auditFlag = response.data.audit === '1'
                    this.authFlagFlag = response.data.auth === '1'
                    this.getInstanceVersions(this.resourceForm.serverCode)
                    this.$nextTick (() => {
                        this.form.setFieldsValue (this.$pick (this.resourceForm,
                          [ 'name', 'alias', 'uri', 'permissions', 'router', 'type', 'parentId', 'icon', 'description', 'serverCode' ]))
                    })
                    let _this = this
                    if(_this.resourceForm.type === '1'){
                        _this.$nextTick (() => {
                            _this.$nextTick (() => {
                                _this.$nextTick (() => {
                                    _this.form.setFieldsValue (_this.$pick (_this.resourceForm,
                                        [ 'version', 'webVersion' ]))
                                })
                            })
                        })
                    }
                    if (this.resourceForm.audit === '1') {
                        // 当条件是审计时 加载审计信息
                        this.getResAudit ()
                    }
                })
            },

            /**
             * Function    : loadParentResourceInfo
             * Description : 加载当前节点的上级节点详情
             * Input       : id 父节点id
             * Return      : none
             */
            loadParentResourceInfo ( id ) {
                let _this = this
                this.$store.dispatch ('loadResourceAct', {id}).then (( response ) => {
                    _this.parentResource = response.data
                    _this.currentParentResName = response.data.name
                })
            },

            /**
             * Function    : addResource
             * Description : 新增资源
             * Input       : none
             * Return      : none
             */
            addResource () {
                this.parentResource = this.resourceForm
                this.form.resetFields ()
                this.form2.resetFields ()
                this.clearResourceForm ()
                if(this.currentSelectedNodeId !== ''){
                    this.form.setFieldsValue({ parentId: this.currentSelectedNodeId })
                }
                this.authFlagFlag = true
            },

            /**
             * Function    : delResource
             * Description : 删除资源
             * Input       : none
             * Return      : none
             */
            delResource () {
                if (!this.currentSelectedNodeId) {
                    this.$message.error ('请选择具体资源后删除')
                    return
                }
                this.$confirm ({
                    title: '确认删除选中的系统资源及其子系统资源吗？',
                    onOk: () => {
                        this.$store.dispatch ('delSysResourceAct', {id: this.currentSelectedNodeId}).then (response => {
                            if (response.code === 0) {
                                this.$message.success (response.message)
                                // 重新加载delSysResource
                                this.loadResourceTree ()
                            } else {
                                this.$message.error (response.message)
                            }
                        })
                    },
                    onCancel: () => {
                    }
                })
            },

            /**
             * Function    : enableResource
             * Description : 启用资源
             * Input       : none
             * Return      : none
             */
            enableResource () {
                this.$store.dispatch ('enableSysResourceAct', {id: this.resourceForm.id}).then (response => {
                    if (response.code === 0) {
                        this.$message.success (response.message)
                        // 重新加载delSysResource
                        this.loadResourceTree (this.resourceForm.id)
                    } else {
                        this.$message.error (response.message)
                    }
                })
            },

            /**
             * Function    : disableResource
             * Description : 停用资源
             * Input       : none
             * Return      : none
             */
            disableResource () {
                this.$store.dispatch ('disableSysResourceAct', {id: this.resourceForm.id}).then (response => {
                    if (response.code === 0) {
                        this.$message.success (response.message)
                        // 重新加载delSysResource
                        this.loadResourceTree (this.resourceForm.id)
                    } else {
                        this.$message.error (response.message)
                    }
                })
            },

            /**
             * Function    : disableResource
             * Description : 切换资源节点
             * Input       : data
             * Return      : none
             */
            changeNode ( data, node, selectedNodes ) {
                if(data.length >0){
                    this.currentSelectedNodeId = data[ 0 ]
                    this.clearAuditForm ()
                    this.loadResourceInfo (this.currentSelectedNodeId)
                }
            },

            /**
             * Function    : saveResource
             * Description : 保存资源
             * Input       : none
             * Return      : none
             */
            saveResource () {
                this.form.validateFields (( err, value ) => {
                    if (!err) {
                        // 新增
                        this.resourceForm.name = value.name
                        this.resourceForm.alias = value.alias
                        this.resourceForm.uri = value.uri
                        this.resourceForm.permissions = value.permissions
                        this.resourceForm.router = value.router
                        this.resourceForm.type = value.type
                        this.resourceForm.parentId = value.parentId
                        this.resourceForm.icon = value.icon
                        this.resourceForm.description = value.description
                        this.resourceForm.serverCode = value.serverCode
                        this.resourceForm.version = value.version
                        this.resourceForm.webVersion = value.webVersion
                        this.resourceForm.auth = this.authFlagFlag?'1':'0'
                        let para = Object.assign ({}, this.resourceForm)
                        if (para.router === undefined) {
                            para.router = ''
                        }
                        if (this.resourceForm.id) {
                            this.$store.dispatch ('updSysResourceAct', para).then (response => {
                                if (response.code === 0) {
                                    this.$message.success (response.message)
                                    this.loadResourceTree (this.resourceForm.id)
                                } else {
                                    this.$message.error (response.message)
                                }
                            })
                        } else {
                            this.$store.dispatch ('addSysResourceAct', para).then (response => {
                                if (response.code === 0) {
                                    this.$message.success (response.message)
                                    this.loadResourceTree (response.data.id)
                                } else {
                                    this.$message.error (response.message)
                                }
                            })
                        }
                    }
                })
            },

            /**
             * Function    : handleReset
             * Description : 重置表单
             * Input       : name
             * Return      : none
             */
            handleReset ( name ) {
                let _this = this
                setTimeout (function () {
                    _this[ name ].resetFields ()
                }, 100)
            },

            /**
             * Function    : getResAudit
             * Description : 加载审计配置详情
             * Input       : none
             * Return      : none
             */
            getResAudit () {
                let _this = this
                setTimeout (function () {
                    _this.form2.resetFields ()
                }, 100)
                let resId = this.resourceForm.id
                this.$store.dispatch ('getResAuditAct', {resId}).then (response => {
                    if (response.code === 0) {
                        this.auditForm = response.data
                        this.$nextTick (() => {
                            this.form2.setFieldsValue (this.$pick (this.auditForm,
                              [ 'auditSort', 'operateType', 'objidParamSeq', 'objidParamName', 'authdevParamSeq', 'authdevParamName', 'shieldParams' ]))
                        })
                    } else {
                        this.$message.error (response.message)
                    }
                })
            },

            /**
             * Function    : saveAudit
             * Description : 保存审计配置
             * Input       : none
             * Return      : none
             */
            saveAudit () {
                this.form2.validateFields (( err, value ) => {
                    if (!err) {
                        let type = this.form.getFieldValue('type')
                        if (type !== '3') {
                            this.$message.error ('只有资源类型为系统功能时可配')
                            return
                        }
                        this.auditForm.auditSort = value.auditSort
                        this.auditForm.operateType = value.operateType
                        this.auditForm.objidParamSeq = value.objidParamSeq
                        this.auditForm.objidParamName = value.objidParamName
                        this.auditForm.authdevParamSeq = value.authdevParamSeq
                        this.auditForm.authdevParamName = value.authdevParamName
                        this.auditForm.shieldParams = value.shieldParams
                        this.auditForm.auditType = value.auditType
                        this.auditForm.resId = this.resourceForm.id
                        if (!this.auditForm.id) {
                            // 新增
                            this.$store.dispatch ('addResAuditAct', this.auditForm).then (response => {
                                if (response.code === 0) {
                                    this.$message.success (response.message)
                                    this.getResAudit ()
                                } else {
                                    this.$message.error (response.message)
                                }
                            })
                        } else {
                            // 修改
                            this.$store.dispatch ('updResAuditDef', this.auditForm).then (response => {
                                if (response.code === 0) {
                                    this.$message.success (response.message)
                                } else {
                                    this.$message.error (response.message)
                                }
                            })
                        }
                    }
                })
            },

            /**
             * Function    : changeAudit
             * Description : 新增或删除审计配置
             * Input       : data
             * Return      : none
             */
            changeAudit ( e ) {
                this.resourceForm.auditFlag = e.target.checked
                if(this.resourceForm.auditFlag){
                    this.resourceForm.audit= '1'
                }else{
                    this.resourceForm.audit= '0'
                }
                let type = this.form.getFieldValue('type')
                if (this.resourceForm.audit === '1' && type !== '3') {
                    this.$message.error ('只有资源类型为系统功能时可配')
                    this.loadResourceTree ()
                    return
                }
                let authDel = this.hasPermission ('sysresource:delResAuditDef')
                let authAdd = this.hasPermission ('sysresource:addResAuditDef')
                if (this.resourceForm.audit === '0' && !authDel) {
                    this.$message.error ('无删除审计配置权限')
                    this.loadResourceInfo (this.resourceForm.id)
                    return
                }
                if (this.resourceForm.audit === '1' && !authAdd) {
                    this.$message.error ('无新增审计配置权限')
                    this.loadResourceInfo (this.resourceForm.id)
                    return
                }
                if (this.resourceForm.audit === '0' && this.auditForm.id) {
                    this.$confirm ({
                        title: '审计状态设置成否后，相关审计配置将会被删除，是否确定？',
                        onOk: () => {
                            this.$store.dispatch ('delResAuditDef', {id: this.auditForm.id}).then (response => {
                                if (response.code === 0) {
                                    this.$message.success (response.message)
                                    this.loadResourceInfo (this.resourceForm.id)
                                    this.clearAuditForm ()
                                } else {
                                    this.$message.error (response.message)
                                }
                            })
                        },
                        onCancel: () => {
                            this.resourceForm.audit = '1'
                            this.resourceForm.auditFlag = true
                        }
                    })
                }
            },

            changeAuthFlag ( e ) {
                this.authFlagFlag= e.target.checked
                if(this.authFlagFlag){
                    this.resourceForm.auth= '1'
                }else{
                    this.resourceForm.auth= '0'
                }
            },

            /**
             * Function    : clearAuditForm
             * Description : 初始化审计配置信息
             * Input       : none
             * Return      : none
             */
            clearAuditForm () {
                this.auditForm = {
                    id: '',
                    resId: '',
                    auditSort: '',
                    operateType: '',
                    objidParamSeq: '',
                    objidParamName: '',
                    authdevParamSeq: '',
                    authdevParamName: '',
                    shieldParams: '0'
                }
            },

            /**
             * Function    : clearResourceForm
             * Description : 清空资源配置信息
             * Input       : none
             * Return      : none
             */
            clearResourceForm () {
                this.resourceForm = {
                    id: '',
                    name: '',
                    alias: '',
                    uri: '',
                    permissions: '',
                    router: '',
                    type: '',
                    parentId: '',
                    icon: '',
                    audit: '0',
                    validStatus: '0',
                    description: ''
                }
            },

            /**
             * Function    : selectNodeId
             * Description :
             * Input       : val
             * Return      : none
             */
            selectNodeId ( val ) {
                this.resourceForm.parentId = val.id
            },
          getNacosServices() {
            let _this = this
            this.$get('api/getNacosServices', {}).then(res => {
              if (res.code === 0) {
                  let newList = res.data
                  for(let i = 0; i<newList.length-1;i++){
                      for(let j = i+1; j<newList.length;j++){
                          if(newList[i].serverCode=== newList[j].serverCode){
                              newList.splice(j,1)
                              j--
                          }
                      }
                  }
                _this.serList = newList
              } else {
                _this.$message.error(res.message)
              }
            })
          },
          getInstanceVersions(value) {
            let _this = this
            this.$get('api/getInstanceVersions', {serviceName:value}).then(res => {
              if (res.code === 0) {
                _this.verionList = res.data
              } else {
                _this.$message.error(res.message)
              }
            })
          },
          /**
           * Function    : cancelApply
           * Description : 取消弹出框
           * Input       : name
           * Return      : none
           */
          cancelApply() {
            this.showAddTable = false
            let _this = this
            setTimeout(function() {
              _this.form3.resetFields()
              _this.serList = []
              _this.verionList = []
            }, 100)
          },

          saveApply(){
            this.form3.validateFields((err, value) => {
              if (!err) {
                let _this = this
                let pram = {
                    serviceCode:value.serverCode,
                  version:value.version,
                }
                this.$get('api/synSysResources', pram).then(res => {
                  if (res.code === 0) {
                    _this.$message.success('同步成功')
                  } else {
                    _this.$message.error(res.message)
                  }
                })
              }
            })
          },


        },

        created () {
            this.loadPageDictDatas ()
            // 加载树
            this.loadResourceTree ()
            this.getNacosServices()
        }
    }
</script>
